package com.demo.rabbitmq.demo.routing;

import com.demo.rabbitmq.util.RabbitMQUtil;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consume2 {
    public static void main(String[] args) throws IOException, TimeoutException {

        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();

        //声明 创建队列
        channel.queueDeclare(Producer.DIRECT_OURUE2_UPDATE,true,false,false,null);
        //列队绑定交换机
        channel.queueBind(Producer.DIRECT_OURUE2_UPDATE, Producer.DIRECT_EXCHAGE,"update");
        channel.queueBind(Producer.DIRECT_OURUE2_UPDATE, Producer.DIRECT_EXCHAGE,"delete");
        //消费者标签
        DefaultConsumer defaultConsumer = new DefaultConsumer(channel){


            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {

                System.out.println("消费者标签："+consumerTag);
                System.out.println("路由："+envelope.getRoutingKey());
                System.out.println("交换机："+envelope.getExchange());
                System.out.println("消息id："+envelope.getDeliveryTag());
                System.out.println("消息："+new String(body,"UTF-8"));
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

        //监听消息
        channel.basicConsume(Producer.DIRECT_OURUE2_UPDATE,true,defaultConsumer);
    }
}
